// pages/subject/subject.js
const app = getApp()
const db = wx.cloud.database();
Component({


  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    colorArrays: ["#85B8CF", "#90C652", "#D8AA5A", "#FC9F9D", "#0A9A84", "#61BC69", "#12AEF3", "#E29AAD"],

  },
  methods: {
    // 获取用户具体点的是哪一个
    showCardView(e) {
      wx.navigateTo({
        url: '../subject/subject?id=' + e.currentTarget.dataset.index
      });
      console.log(e.currentTarget.dataset.index)

    },

    addData() {
      db.collection("teacher_users").add({
        data: {
          class_list: [
            //上课长度全部默认为两节课
            {
              "weak": 1,
              "class_order": 1,
              "class_length": 2,
              "subject": "单片机",
              "address": "五教208",
              "weak_work": "1~16周"
            },

            {
              "weak": 2,
              "class_order": 8,
              "class_length": 2,
              "subject": "汇编语音",
              "address": "五教208",
              "weak_work": "1~16周"
            },

            {
              "weak": 3,
              "class_order": 3,
              "class_length": 2,
              "subject": "单片机",
              "address": "五教208",
              "weak_work": "1~16周"
            },
            {
              "weak": 3,
              "class_order": 5,
              "class_length": 4,
              "subject": "C语言",
              "address": "五教208",
              "weak_work": "1~16周"
            },

            {
              "weak": 6,
              "class_order": 9,
              "class_length": 2,
              "subject": "C语言实验课",
              "address": "五教208",
              "weak_work": "1~16周"
            },

          ],
          name: "张勇辉",
          phtot: "17325647895",
          teacher: "123456",
          institute: "信息与通信工程学院",
        }
      }).then(res => {
        console.log(res)
        console.log('更新数据库完成')
      })

    },
  },

  attached() {
    // 查询用户openid


    if (!app.globalData.allInfo) {
      wx.cloud.callFunction({
        name: 'login',
        data: {},
        success: res => {
          app.globalData.openid = res.result.openid
          console.log(app.globalData)
          console.log('[云函数] [login] user openid: ', app.globalData.openid)
          // 当用户是学生时
          if (app.globalData.isStudent == '1') {
            // 查询用户信息
            db.collection("users").where({
              openid: app.globalData.openid
            }).get().then(res => {
              app.globalData.allInfo = res.data[0]
              console.log(res)
              console.log(app.globalData)
              this.setData({
                user: app.globalData.allInfo,
                isStudent: true
              })
              if (!app.globalData.class) {
                // 查询用户课表信息
                db.collection("class").where({
                  grade: app.globalData.allInfo.grade,
                  class: app.globalData.allInfo.class,
                  major: app.globalData.allInfo.major,

                }).get().then(res => {
                  console.log(res.data[0].class_list)
                  console.log("查询课程表")
                  app.globalData.class = res.data[0].class_list
                  this.setData({
                    class_list: res.data[0].class_list
                  })
                })
              } else {
                this.setData({
                  class_list: app.globalData.class
                })
              }
            })
          }
          // 当用户是老师时
          else {
            // 查询用户信息
            db.collection("teacher_users").where({
              openid: app.globalData.openid
            }).get().then(res => {
              app.globalData.allInfo = res.data[0]
              console.log(res)
              console.log(app.globalData)
              this.setData({
                user: app.globalData.allInfo,
                isStudent: false

              })
              if (!app.globalData.class) {
                app.globalData.class = res.data[0].class_list
                this.setData({
                  class_list: res.data[0].class_list
                })
              } else {
                this.setData({
                  class_list: app.globalData.class
                })
              }
            })
          }


        },
        fail: err => {
          console.error('[云函数] [login] 调用失败', err)
        }
      })
    } else {
      if (!app.globalData.class) {
        // 当用户是学生时
        if (app.globalData.isStudent == '1') {
          // 查询用户课表信息
          db.collection("class").where({
            grade: app.globalData.allInfo.grade,
            class: app.globalData.allInfo.class,
            major: app.globalData.allInfo.major,

          }).get().then(res => {
            console.log(res)
            console.log("查询课程表")
            app.globalData.class = res.class_list
            this.setData({
              class_list: res.data[0].class_list,
              isStudent:true
            })
          })
        } else {
          app.globalData.class = app.globalData.allInfo.class_list
          console.log(app.globalData)
          this.setData({
            class_list: app.globalData.allInfo.class_list,
            isStudent: false

          })
        }
      } else {
        this.setData({
          class_list: app.globalData.class
        })
      }
    }





  },




  tapName: function (event) {
    console.log(event)
  }


})